package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.http.User;
import com.diudiutechnology.www.yadiudiu.util.http.UserCallback;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.LazyFragment;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CallSingleBannerBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleaningActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CreditTutorialActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.ExpressDeliveryActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.ResumeTheQueryActivity;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/8/16.
 * 叫单首页板块
 */

public class CalledSingleFragment extends LazyFragment implements View.OnClickListener {
    private Context context;
    private LinearLayout resume_the_query_layout;//简历查询
    private LinearLayout cleaning_layout;//干洗
    private LinearLayout credit_tutorial_layout;//信贷教程
    private LinearLayout fruits_and_vegetables_layout;//蔬果
    private LinearLayout coolie_layout;//苦力
    private LinearLayout the_lock_layout;//开锁
    private LinearLayout housekeeping_layout;//家政
    private LinearLayout express_delivery_layout;//快递

   private ViewPager ad_pager;//轮播
    private LinearLayout ad_layer; // 圈圈 布局
    private final int HOME_AD_RESULT = 1;
    private CallSingleBannerBean mBean;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    ad_pager.setCurrentItem(ad_pager.getCurrentItem() + 1,
                            true);
                    break;
            }
        };
    };
    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_called_single);
         initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=getActivity();
    }
    private void initView(){
        ad_pager = (ViewPager)findViewById(R.id.ad_pager);
        ad_layer = (LinearLayout)findViewById(R.id.ad_layer);
        //简历查询
        resume_the_query_layout= (LinearLayout)findViewById(R.id.resume_the_query_layout);
        //干洗
        cleaning_layout= (LinearLayout)findViewById(R.id.cleaning_layout);
        //信贷教程
        credit_tutorial_layout= (LinearLayout)findViewById(R.id.credit_tutorial_layout);
        //蔬果
        fruits_and_vegetables_layout= (LinearLayout)findViewById(R.id.fruits_and_vegetables_layout);
        //苦力
        coolie_layout= (LinearLayout)findViewById(R.id.coolie_layout);
        //开锁
        the_lock_layout= (LinearLayout)findViewById(R.id.the_lock_layout);
        //家政
        housekeeping_layout= (LinearLayout)findViewById(R.id.housekeeping_layout);
        //快递
        express_delivery_layout= (LinearLayout)findViewById(R.id.express_delivery_layout);

        credit_tutorial_layout.setOnClickListener(this);
        resume_the_query_layout.setOnClickListener(this);
        cleaning_layout.setOnClickListener(this);
        fruits_and_vegetables_layout.setOnClickListener(this);
        the_lock_layout.setOnClickListener(this);
        coolie_layout.setOnClickListener(this);
        housekeeping_layout.setOnClickListener(this);
        express_delivery_layout.setOnClickListener(this);
        initData();
    }

    private void initData(){
        CleanBusiness.getCallBanner(context, new UserCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(User response, int id) {
                mBean = CleanBusiness.parseToCallSingleBannerBean(response.toString());
                if(mBean!=null){
                    if (mBean.figurepic.length() > 0) {
                        List<String> pic = new ArrayList<>();
                        String[] split = mBean.figurepic.split("\\|");
                        for (int i = 0; i < split.length; i++) {
                            String s = split[i];
                            pic.add(i, s);
                        }
                        getadvertising(pic.size(), pic);
                    }
                }
            }
        });
    }
    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult){
        ad_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(ad_layer,position % size,0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT,  3*1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult,0);
        ad_pager.setAdapter(adapter);
        Sundry.initPointsLayout(ad_layer,context,size,0);
        ad_pager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3*1000);
    }
    @Override
    public void onClick(View view) {
        Intent intent=new Intent();
        switch (view.getId()){
            case R.id.resume_the_query_layout:
                intent.setClass(context, ResumeTheQueryActivity.class);//简历
                startActivity(intent);
                break;
            case R.id.credit_tutorial_layout:  //信贷教程
                intent.setClass(context, CreditTutorialActivity.class);
                startActivity(intent);
                break;
            case R.id.coolie_layout:
                intent.setClass(context, CleaningActivity.class);//苦力
                intent.putExtra("pos","0");
                startActivity(intent);
                break;
            case R.id.cleaning_layout:
                intent.setClass(context, CleaningActivity.class);//干洗
                intent.putExtra("pos","1");
                startActivity(intent);
                break;
            case R.id.the_lock_layout:
                intent.setClass(context, CleaningActivity.class);//开锁
                intent.putExtra("pos","2");
                startActivity(intent);
                break;
            case R.id.housekeeping_layout:
                intent.setClass(context, CleaningActivity.class);//家政
                intent.putExtra("pos","3");
                startActivity(intent);
                break;
            case R.id.fruits_and_vegetables_layout:  //蔬果
                intent.setClass(context, CleaningActivity.class);
                intent.putExtra("pos","4");
                startActivity(intent);
                break;
            case R.id.express_delivery_layout:
                intent.setClass(context, ExpressDeliveryActivity.class);//快递
                startActivity(intent);
                break;

        }

    }
}

